<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            position: absolute;
            width: 50px;
            height: 50px;
            background-color: pink;
        }
    </style>
</head>

<body>
    <div></div>
    <script>
        var div = document.querySelector('div')
        animate(div,500); 
        function animate(obj, target,callback) {
            // 防止定时器多开
                clearInterval(obj.animal)
            // 防止变量名重复
            obj.animal = setInterval(function () {
                var step  = (target-obj.offsetLeft)/10
                step>0 ? Math.ceil(step) : Math.floor(step)
                obj.style.left = obj.offsetLeft + step + 'px'
                if (obj.offsetLeft >= target) {
                    clearInterval(obj.animal)
                    if(callback){
                              callback()
                    }

                }
            }, 30)
        }
    </script>
</body>

</html>